package com.example.xm.adapter.HomeAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.base.BaseAdapter;
import com.example.xm.bean.HomeBean.HomeSquareBean;
import com.example.xm.customizing.ExpandableTextView;
import com.example.xm.ui.home.activity.ImageActivity;
import com.example.xm.utiles.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class HomeSquareAdapter extends BaseAdapter {

    public HomeSquareAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_square_item;
    }

    @SuppressLint("NewApi")
    @Override
    protected void bindData(BaseViewHolder viewHolder, Object o) {
        ImageView img = (ImageView) viewHolder.getViewById(R.id.iv_head_square_item);
        TextView title = (TextView) viewHolder.getViewById(R.id.tv_title_square);
        ImageView lv = (ImageView) viewHolder.getViewById(R.id.iv_lv_square);
        TextView guanzhu = (TextView) viewHolder.getViewById(R.id.tv_guanzhu_square);
        TextView date = (TextView) viewHolder.getViewById(R.id.tv_date_square);
        ExpandableTextView des = (ExpandableTextView) viewHolder.getViewById(R.id.txt_des2);
        RecyclerView rlv = (RecyclerView) viewHolder.getViewById(R.id.rlv_sqare_item);
        HomeSquareBean.DataBean.DynamicsBean bean = (HomeSquareBean.DataBean.DynamicsBean) o;
        RequestOptions requestOptions = new RequestOptions().circleCrop();
        Glide.with(context).load(bean.getHeadUrl()).apply(requestOptions).into(img);
        title.setText(bean.getNickName());
        int level = bean.getLevel();
        if (level == 1) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv1));
        } else if (level == 2) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv2));
        } else if (level == 3) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv3));
        } else if (level == 4) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv4));
        } else if (level == 5) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv5));
        } else if (level == 6) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv6));
        } else if (level == 7) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv7));
        } else if (level == 8) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv8));
        } else if (level == 9) {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv9));
        } else {
            lv.setImageDrawable(context.getDrawable(R.mipmap.lv10));
        }
        if (!TextUtils.isEmpty(bean.getCreateTime())) {
            Long dateToTime = DateUtils.getDateToTime(bean.getCreateTime(), null);
            String standardDate = DateUtils.getStandardDate(dateToTime);
            date.setText(standardDate);
        }
        des.setMaxLines(70);
        des.setHasAnimation(false);
        des.setCloseInNewLine(true);
        des.setOpenSuffixColor(context.getResources().getColor(R.color.colorAccent));
        des.setCloseSuffixColor(context.getResources().getColor(R.color.colorAccent));
        des.setOriginalText(bean.getContent());
        //显示嵌套的GridView效果的图片
        List<HomeSquareBean.DataBean.DynamicsBean.ImagesBean> images = bean.getImages();
        //音频

        HomeSquareImageAdapter imageAdapter = new HomeSquareImageAdapter(context, images);
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        
        rlv.setLayoutManager(manager);
//        rlv.addItemDecoration(new DividerItemDecoration(context, LinearLayout.VERTICAL));
//        rlv.addItemDecoration(new DividerItemDecoration(context,LinearLayout.HORIZONTAL));
        rlv.setAdapter(imageAdapter);
        List<HomeSquareBean.DataBean.DynamicsBean.ImagesBean> imagesBeans = bean.getImages();
        ArrayList<String> imgs = new ArrayList<>();
        for (int i = 0; i < imagesBeans.size(); i++) {
            imgs.add(imagesBeans.get(i).getFilePath());
        }
        rlv.setAdapter(imageAdapter);
        imageAdapter.setOnClick(new
                                        onClick() {
                                            @Override
                                            public void click(int pos) {
                                                Intent intent = new Intent(context, ImageActivity.class);
                                                intent.putExtra("postion", pos);
                                                intent.putExtra("list", imgs);
                                                context.startActivity(intent);

                                            }
                                        });

    }
}
